package com.agw.crm.web.listener;

import com.agw.crm.settings.domain.DicType;
import com.agw.crm.settings.domain.DicValue;
import com.agw.crm.settings.service.DicService;
import com.agw.crm.settings.service.impl.DicServiceImpl;
import com.agw.crm.utils.ServiceFactory;
import com.agw.crm.utils.SqlSessionUtil;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.*;

public class SysInitListener implements ServletContextListener {
    //监听器方法，负责监听上下文作用域对象的方法，服务器启动上下文作用域对象创建，作用域对象创建将数据
    //储存到服务器缓存中
    //方法参数event可以获取到监听的对象
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("服务器缓存处理数据字典开始");

        //System.out.println("上下文作用域对象创建了");
        //获取到上下文作用域对象
        ServletContext application = event.getServletContext();

        //调用service层进行查询
        DicService dicService = (DicService) ServiceFactory.getService(new DicServiceImpl());

        Map<String, List<DicValue>> map = dicService.getAllCode();
        //遍历map集合
        Set<String> keySet = map.keySet();

        for (String key:keySet){
            //将map集合存储到application对象中
            application.setAttribute(key,map.get(key));
        }
        //application.setAttribute("key","数据字典");

        System.out.println("服务器缓存处理数据字典结束");
        
        
        /*
        将properties文件解析后保存到applaction作用域对象中
        
         */
        System.out.println("将Stage2Possibility.properties文件保存到服务器缓存中");
        //解析文件
        ResourceBundle rb = ResourceBundle.getBundle("Stage2Possibility");
        //获取到文件所有的Key
        Enumeration<String> e = rb.getKeys();

        //用一个Map集合保存
        Map<String,String> pMap = new HashMap<String,String>();
        //遍历Key获取到所有的value
        while (e.hasMoreElements()){
            //阶段
            String key = e.nextElement();//所有的Key
            //可能性
            String value = rb.getString(key);//所有的Value

            pMap.put(key,value);

        }
        //将pMap保存在服务器缓存中
        application.setAttribute("pMap",pMap);
    }


}
